﻿using System;


using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DalEntity
{
    public partial class Caminhao : Modelo.Interface.IBaseBD
    {
        ShippingEntities db;
        public IList<T> RetornaGeral<T>() where T : class, new()
        {
            db = new ShippingEntities();
            List<T> retorno = new List<T>();
            foreach (Caminhao C in db.Caminhao.ToList())
            {
                T valor = GenericTypeConverter.ConvertType<Caminhao, T>(C);
                retorno.Add(valor);
            }
            return retorno;
        }

        public T RetornaBasePorID<T>(int id) where T : class, new()
        {
            db = new ShippingEntities();
            return GenericTypeConverter.ConvertType<Caminhao, T>(db.Caminhao.SingleOrDefault(c => c.Id == id));
        }

        public object RetornaInstancia<T>(T obj) where T : class, new()
        {
            
            Caminhao Cam =  GenericTypeConverter.ConvertType<T, Caminhao>(obj);

            if (Cam.IdMotorista != null)
            {
                int id = Cam.IdMotorista.Value;

                Cam.Motorista = db.Motorista.SingleOrDefault(c => c.Id == id);
                    
            }
            return Cam;
        }
    }
}
